package com.uam.core.business.help;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uam.core.entity.uitls.DecimalFormatUtils;
import com.uam.core.lk.utils.fencebuffer.BufferBase;
import com.uam.core.lk.utils.fencebuffer.PolygonBufferImpl;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.operation.buffer.BufferOp;
import org.locationtech.jts.operation.overlay.snap.GeometrySnapper;
import org.locationtech.jts.operation.polygonize.Polygonizer;

import java.util.Collection;

public class TestInfo {
    static PolygonBufferImpl polygonBuffer = new PolygonBufferImpl();

    public static void main(String[] args) {
        Point point = BufferBase.geometryFactory.createPoint(new Coordinate(112.963999, 28.195999));

        JSONArray jsonArrayLint = JSONArray.parseArray("[[112.97032,28.189194,0.0,[],1720840265000],[112.967999,28.191999],[112.867999,28.19599],[112.867521,28.196477,0.0,[],1720840434000]]");
        Coordinate[] coordinatesLine = new Coordinate[jsonArrayLint.size()];
        for (int i = 0; i < jsonArrayLint.size(); i++) {
            coordinatesLine[i] = new Coordinate(jsonArrayLint.getJSONArray(i).getDouble(0), jsonArrayLint.getJSONArray(i).getDouble(1));
        }

        LineString lineString = BufferBase.geometryFactory.createLineString(coordinatesLine);

        JSONArray poly = JSONArray.parseArray("[[112.86797923074617,28.19550476865434],[112.86793623087414,28.195508372357825],[112.86789370871304,28.195515710033916],[112.86785198788222,28.195526725838466],[112.86781138590239,28.195541335934553],[112.86777221177911,28.195559429130522],[112.86773476365114,28.19558086772623],[112.86769932652133,28.19560548856101],[112.86766617008765,28.195633104255457],[112.86763554669056,28.19566350463745],[112.86760768939263,28.195696458341732],[112.8675828102047,28.195731714570712],[112.8675610984724,28.1957690050032],[112.86754271943514,28.195808045836486],[112.86752781296849,28.19584853994626],[112.86751649251967,28.195890179147895],[112.86750884424411,28.19593264654192],[112.86750492634982,28.19597561892583],[112.86750476865434,28.196018769253826],[112.86750837235782,28.19606176912587],[112.86751571003391,28.19610429128696],[112.86752672583846,28.19614601211778],[112.86754133593455,28.196186614097616],[112.86755942913052,28.196225788220886],[112.86758086772622,28.196263236348855],[112.86760548856101,28.196298673478662],[112.86763310425545,28.196331829912346],[112.86766350463745,28.19636245330943],[112.86769645834173,28.19639031060737],[112.86773171457071,28.196415189795307],[112.8677690050032,28.1964369015276],[112.86780804583648,28.196455280564855],[112.86784853994625,28.196470187031505],[112.86789017914789,28.196481507480332],[112.86793264654192,28.196489155755888],[112.86797561892583,28.196493073650178],[112.86801876925382,28.19649323134566],[112.96801876925383,28.192493231345658],[112.96806176912587,28.192489627642175],[112.96810429128696,28.192482289966083],[112.96814601211778,28.192471274161534],[112.96818661409762,28.192456664065446],[112.96822578822089,28.192438570869477],[112.96826323634886,28.19241713227377],[112.96829867347867,28.19239251143899],[112.96833182991236,28.192364895744543],[112.96836245330944,28.192334495362548],[112.96839031060738,28.192301541658267],[112.96841518979531,28.192266285429287],[112.96843690152761,28.1922289949968],[112.96845528056487,28.192189954163513],[112.96847018703151,28.192149460053738],[112.96848150748033,28.192107820852105],[112.96848915575589,28.19206535345808],[112.96849307365018,28.19202238107417],[112.96849323134566,28.191979230746174],[112.96848962764219,28.19193623087413],[112.96848228996609,28.19189370871304],[112.96847127416154,28.19185198788222],[112.96845666406546,28.191811385902383],[112.96843857086948,28.191772211779114],[112.96841713227379,28.191734763651144],[112.96839251143899,28.191699326521338],[112.96836489574456,28.191666170087654],[112.96833449536256,28.19163554669057],[112.96830154165828,28.19160768939263],[112.9682662854293,28.191582810204693],[112.9682289949968,28.1915610984724],[112.96818995416352,28.191542719435144],[112.96814946005375,28.191527812968495],[112.96810782085211,28.191516492519668],[112.96806535345809,28.19150884424411],[112.96802238107418,28.191504926349822],[112.96797923074618,28.19150476865434],[112.86797923074617,28.19550476865434]]");


        Coordinate[] coordinatespoly = new Coordinate[poly.size()];
        for (int i = 0; i < poly.size(); i++) {
            coordinatespoly[i] = new Coordinate(poly.getJSONArray(i).getDouble(0), poly.getJSONArray(i).getDouble(1));
        }
        Polygon polygon = BufferBase.geometryFactory.createPolygon(coordinatespoly);
        test( lineString, polygon);
    }

    public static JSONArray test(LineString intersectionLine, Polygon polygon) {
        Geometry geometryd = polygon.intersection(intersectionLine);
        Coordinate coordinate=geometryd.getCoordinates()[0];
        Coordinate coordinate2= geometryd.getCoordinates()[ geometryd.getCoordinates().length-1];

        String key1= DecimalFormatUtils.getDecimalFormatUtils(coordinate.x)+"-"+DecimalFormatUtils.getDecimalFormatUtils(coordinate.y);
        String key2=DecimalFormatUtils.getDecimalFormatUtils(coordinate2.x)+"-"+DecimalFormatUtils.getDecimalFormatUtils(coordinate2.y);
//        System.out.println(coordinate.x+" "+coordinate.y);
//        System.out.println(coordinate2.x+" "+coordinate2.y);
        // 获取面的边界
        Geometry polygonBoundary = polygon.getBoundary();

        // 将交叉线段与面的边界进行交集操作，以添加交叉点
        // 更新面
        GeometryCollection updatedPolygon;
        Geometry updatedBoundary;
        try {
            updatedBoundary = polygonBoundary.union(intersectionLine);
            updatedPolygon= (GeometryCollection)polygon.difference(polygonBoundary).union(updatedBoundary);
        }catch (Exception e){
            System.out.println("跟新错误"+intersectionLine);
            return null;
        }



        int row= updatedPolygon.getNumGeometries();
        for(int i=0;i<row;i++){
           Geometry geometry= updatedPolygon.getGeometryN(i);
           if(geometry instanceof Polygon){
//               System.out.println(GeoSubStringUtils.lonlatTemp(geometry.toText()));
               int startIndex=0;
               int endIndex=0;
              Coordinate[] coordinates= geometry.getCoordinates();
              for (int j=0;j<coordinates.length;j++){
                  Coordinate coordinate1= coordinates[j];
                  String temp=DecimalFormatUtils.getDecimalFormatUtils(coordinate1.x)+"-"+DecimalFormatUtils.getDecimalFormatUtils(coordinate1.y);
                  if(key1.equals(temp)){
                      startIndex=j;
                  }
                  if(key2.equals(temp)){
                      endIndex=j;
                  }
              }
//               System.out.println("startIndex:"+startIndex+" endIndex:"+endIndex);
               JSONArray jsonArray=new JSONArray(endIndex);
              if(startIndex>endIndex){

                  for(int z=startIndex;z<coordinates.length;z++){
                      JSONArray jsonArray1=new JSONArray(3);
                      Coordinate coordinate1=coordinates[z];
                      jsonArray1.add(coordinate1.x);
                      jsonArray1.add(coordinate1.y);
                      jsonArray1.add(0);
                      jsonArray.add(jsonArray1);
                  }
                  for(int j=0;j<endIndex;j++){
                      JSONArray jsonArray1=new JSONArray(3);
                      Coordinate coordinate1=coordinates[j];
                      jsonArray1.add(coordinate1.x);
                      jsonArray1.add(coordinate1.y);
                      jsonArray1.add(0);
                      jsonArray.add(jsonArray1);
                  }
              }else {
                  for (int h = startIndex; h <= endIndex; h++) {
                      JSONArray jsonArray1 = new JSONArray(3);
                      Coordinate coordinate1 = coordinates[h];
                      jsonArray1.add(coordinate1.x);
                      jsonArray1.add(coordinate1.y);
                      jsonArray1.add(0);
                      jsonArray.add(jsonArray1);
                  }

              }
//               System.out.println(JSONObject.toJSONString(jsonArray));
               return jsonArray;
           }
//            System.out.println();
        }
      return new JSONArray();
    }

    // 使用线分割多边形
    private static Geometry[] splitPolygonByLine(Polygon polygon, LineString line) {
        GeometryFactory geometryFactory = new GeometryFactory();

        // 使用GeometrySnapper修正多边形和线的交点
        Geometry[] snappedLine = GeometrySnapper.snap(line, polygon, 0.001);
        for (int i = 0; i < snappedLine.length; i++){
            System.out.println(GeoSubStringUtils.lonlatTemp(snappedLine[i].toText()));
        }

        // 使用线分割多边形
//        Geometry nodedLinework = polygon.getBoundary().union(snappedLine[0]);

        // 构造多边形
//        Polygonizer polygonizer = new Polygonizer();
//        polygonizer.add(nodedLinework);
//
//        @SuppressWarnings("unchecked")
//        Collection<Polygon> polygons = polygonizer.getPolygons();
//        polygons.forEach(a->{
//            System.out.println(GeoSubStringUtils.lonlatTemp(a.toText()));
//        });
//
//        if (polygons.size() == 2) {
//            return polygons.toArray(new Polygon[0]);
//        }

        return null;
    }
}
